
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.miage.shopping.presentation;

import fr.miage.shopping.dao.traitements.CategorieDao;
import fr.miage.shopping.metier.entite.Categorie;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author MACAJ
 */
public class ModificationCategorie extends JFrame implements ActionListener {

    private JButton bOK = new JButton("OK");
    private JButton bAnnuler = new JButton("Annuler");
    private JLabel lNomCategorie = new JLabel("Nom categorie");
    private JTextField tNomCategorie = new JTextField(30);
    private JComboBox categories;
    private Categorie categorie;

    public ModificationCategorie(Categorie categorie) {
        super("Modification categorie");
        this.categorie = categorie;
        initFrame();
    }

    /**
     * Initialisation de la frame
     */
    public void initFrame() {
        this.tNomCategorie.setText(categorie.getNomCate());
        Container pane = this.getContentPane();

        JPanel panelNorth = new JPanel();
        panelNorth.setLayout(new FlowLayout());
        panelNorth.add(lNomCategorie);
        panelNorth.add(tNomCategorie);
        Vector<String> nom = getCategories();
        String nomPere;
        categories = new JComboBox(nom);
        if (categorie.getCategorie() != null) {
            nomPere = categorie.getCategorie().getNomCate();

            categories.setSelectedItem(nomPere);
        } else {
            nomPere = "";
        }
        panelNorth.add(categories);
        pane.add(panelNorth, BorderLayout.NORTH);

        JPanel panelSouth = new JPanel();
        panelSouth.setLayout(new FlowLayout());
        this.bOK.addActionListener(this);
        this.bOK.setActionCommand("ok");
        panelSouth.add(bOK);
        this.bAnnuler.addActionListener(this);
        this.bAnnuler.setActionCommand("annuler");
        panelSouth.add(bAnnuler);
        pane.add(panelSouth, BorderLayout.SOUTH);
        this.pack();
        this.setVisible(true);

    }

    public Vector<String> getCategories() {
        List<Categorie> list = CategorieDao.selectAllCategorieTrie();
        Vector<String> categoriesVector = new Vector<String>();
        categoriesVector.add("");
        for (int i = 0; i < list.size(); i++) {
            categoriesVector.add(list.get(i).getNomCate());
        }
        return categoriesVector;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equalsIgnoreCase("annuler")) {
            this.setVisible(false);
        //afficher la fenetre mere
        } else {

            String nomCategorie = this.tNomCategorie.getText();
            if (nomCategorie.length() < 1) {
                JOptionPane.showMessageDialog(null, "Le nom de la catégorie est incorrect", "Erreur formulaire", JOptionPane.INFORMATION_MESSAGE);
            } else {

                String nomCatePere = (String) categories.getSelectedItem();
                Categorie c = new Categorie();
                c.setNomCate(nomCatePere);
                Categorie pere = CategorieDao.selectCategorieByNom(c);
                Categorie newCate = new Categorie();
                newCate.setIdCate(categorie.getIdCate());
                newCate.setNomCate(nomCategorie);
                newCate.setCategorie(pere);
                CategorieDao.insertCategorie(newCate);
                this.setVisible(false);
            }
        }
    }
}